//
//  HNChoicenessViewController.m
//  HappyNews
//
//  Created by lanou on 16/1/15.
//  Copyright © 2016年 BPWBANP. All rights reserved.
//

#import "HNChoicenessViewController.h"

#import "HNRequestManager.h"

#import "HNChoicenessModel.h"

#import "HNChoicensessTableViewCell.h"

#import "KrVideoPlayerController.h"



@interface HNChoicenessViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *HNHeaderArr;

@property (nonatomic, strong) UITableView *HNChoicenessTabelView;

@property (nonatomic, strong) KrVideoPlayerController  *videoController;

@property (nonatomic, assign) NSInteger index;

@property (nonatomic, strong) UIButton *backButton;

@end

@implementation HNChoicenessViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    _HNHeaderArr = [NSMutableArray array];
    
    
    self.navigationController.navigationBarHidden = YES;
    
    self.tabBarController.tabBar.hidden = YES;

    [self setHNChoicenessUpData];
    
    [self addHNTabelView];
    
    
}

- (void)setHNChoicenessUpData
{
    
    NSString *ChoicenessURL = [NSString stringWithFormat:@"http://vcsp.ifeng.com/vcsp/appData/getGuidRelativeVideoList.do?pageSize=20&guid=%@",_guid];
    
    //NSLog(@"%@",ChoicenessURL);
    
    [HNRequestManager requestWithUrlString:ChoicenessURL parDic:nil method:GET finish:^(NSData *data) {
        NSDictionary *HNDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *HNArr = HNDic[@"guidRelativeVideoInfo"];
        for (NSMutableDictionary *HNOneDic in HNArr) {
            HNChoicenessModel *model = [[HNChoicenessModel alloc]init];
            [model setValuesForKeysWithDictionary:HNOneDic];
            [_HNHeaderArr addObject:model];
        }
        
        _index = 0;
        
        HNChoicenessModel *model = _HNHeaderArr[_index];
        
        [self addVideoPlayerWithURL:[NSURL URLWithString:model.mediaUrl]];
        
        [self addButton];
        
        [_HNChoicenessTabelView reloadData];
   
    } error:^(NSError *error) {
        
    }];
    
}

- (void)addButton
{
    _backButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    
    _backButton.frame = CGRectMake(10, 10, 30, 30);
    
    [_backButton addTarget:self action:@selector(ButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
   
    [_backButton setBackgroundImage:[UIImage imageNamed:@"iconfont-unie642"] forState:(UIControlStateNormal)];
    [self.videoController.view addSubview:_backButton];
    
}

- (void)ButtonAction:(UIButton *)button
{
    __weak typeof(self)weakSelf = self;
    [self.videoController setDimissCompleteBlock:^{
        weakSelf.videoController = nil;
    }];
    

    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

// 视频播放
- (void)addVideoPlayerWithURL:(NSURL *)url{
    if (!self.videoController) {
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        self.videoController = [[KrVideoPlayerController alloc] initWithFrame:CGRectMake(0, 20, width, width*(9.0/16.0))];
        __weak typeof(self)weakSelf = self;
        [self.videoController setDimissCompleteBlock:^{
            weakSelf.videoController = nil;
        }];
        [self.videoController setWillBackOrientationPortrait:^{
            [weakSelf toolbarHidden:NO];
        }];
        [self.videoController setWillChangeToFullscreenMode:^{
            [weakSelf toolbarHidden:YES];
        }];
        [self.view addSubview:self.videoController.view];
    }
    self.videoController.contentURL = url;
    
}
//隐藏navigation tabbar 电池栏
- (void)toolbarHidden:(BOOL)Bool{
    self.navigationController.navigationBar.hidden = Bool;
    self.tabBarController.tabBar.hidden = Bool;
    [[UIApplication sharedApplication] setStatusBarHidden:Bool withAnimation:UIStatusBarAnimationFade];
}

- (void)addHNTabelView
{
    _HNChoicenessTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, kScreenWidth * (9.0/16.0), kScreenWidth, kScreenHeight - 183) style:(UITableViewStylePlain)];
    _HNChoicenessTabelView.delegate = self;
    _HNChoicenessTabelView.dataSource = self;
    
    [_HNChoicenessTabelView registerClass:[HNChoicensessTableViewCell class] forCellReuseIdentifier:@"HNChoicensessTableViewCell"];
    
    _HNChoicenessTabelView.rowHeight = kScreenHeight / 6;
    
    [self.view addSubview:_HNChoicenessTabelView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _HNHeaderArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HNChoicensessTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HNChoicensessTableViewCell" forIndexPath:indexPath];
    
    HNChoicenessModel *model = _HNHeaderArr[indexPath.row];
    
    cell.model = model;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HNChoicenessModel *model = _HNHeaderArr[indexPath.row];
    
    [self addVideoPlayerWithURL:[NSURL URLWithString:model.mediaUrl]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
